﻿using System.ComponentModel.Composition;
using AppsGenerator.Generators;
using AppsGenerator.Utils;
using DBBuilder.DBDataStructure;
using System.IO;
using AppsGenerator.Models;

namespace AppsGenerator.WebGenerators.WebRepositoryGenerator
{
    /// <summary>
    /// The generator is responsible for generating repository components
    /// </summary>
    [Export(typeof(RepositoryGenerator))]
    public sealed class RepositoryGenerator : BaseGenerator, IWebComponentsGenerator
    {
        #region Constructors

        /// <summary>
        /// Default constructor
        /// </summary>
        public RepositoryGenerator()
        {
            this.DirPath = Path.Combine(this.GetAppPath(), typeof(WebGenerator).Name, "Repositories");
            this.InitializeDir(this.DirPath);
        }

        #endregion

        #region Methods

        /// <summary>
        /// Override this method to generate components
        /// </summary>
        /// <param name="db">Data source for generating</param>
        public void Generate(DataBase db)
        {
            foreach (Table tbl in db.Tables)
            {
                // Skips a Table if it is existent in Skip array
                if (!this.IsItemExistent(BaseConsts.SkipTables, tbl.Name))
                {
                    // Generate contents for each partial
                    PartialRepositoryGenerator Generator;

                    if(tbl.Name.Equals(WebConfiguration.Instance.Settings.UserTable))
                    {
                        Generator = IoC.Get<PartialRepositoryUserGenerator>();
                    }
                    else
                    {
                        Generator = IoC.Get<PartialRepositoryCommonGenerator>();
                    }

                    // Start to generate contents
                    if (Generator != null)
                    {
                        Generator.DirPath = this.DirPath;
                        Generator.GenerateContents(tbl);
                    }
                }
            }
        }

        #endregion
    }
}

